import json as js

import requests
from flask import Flask
from flask import jsonify
from flask import Response
from flask import request
from flask import session

app = Flask(__name__)
app.secret_key = "ahidljamlad"


@app.get("/")
def index():
    return "<h1>处理requests请求</h1>"


@app.get("/response_type")
def response_type():
    # return "你给我的giao，giao"
    # return "你给我的giao，giao".encode(encoding="GBK")
    # return json.dumps({"info": "奥力给"})
    return jsonify({"info": "奥力给"})
    # return Response("奥力给奥力给".encode(encoding="GBK"), content_type="text/plain;charset=gbk")


@app.get("/params")
def params():
    # print(request.args)
    # page = request.args.get("page", 1)
    # size = request.args.get("size", 10)
    result = {
        "code": "0",
        "args": request.args,
        "headers": {
            "User-Agent": request.headers["User-Agent"]
        }
    }
    return jsonify(result)


@app.post("/data")
def data():
    # print(request.args)
    # page = request.args.get("page", 1)
    # size = request.args.get("size", 10)
    result = {
        "code": "0",
        "form": request.form,
        "args": request.args,
        "headers": {
            "User-Agent": request.headers["User-Agent"]
        }
    }
    return jsonify(result)


@app.post("/json")
def json():
    name = request.json.get("name")
    password = request.json.get("password")
    if not name or not password:
        return jsonify({
            "msg": "参数错误"
        }), 400
    result = {
        "code": "0",
        "json": request.json,
        "headers": {
            "User-Agent": request.headers["User-Agent"]
        }
    }
    return jsonify(result)


@app.get("/headers")
def headers():
    ua = request.headers["User-Agent"]
    if ua.startswith("python"):
        result = {
            "code": 0,
            "msg": "未知错误"
        }
    else:
        referer = request.headers["Referer"]
        if not referer or not referer.startswith("http://127.0.0.1:5000"):
            result = {
                "code": 0,
                "msg": "未知错误"
            }
        else:
            result = {
                "code": 0,
                "headers": {
                    "User-Agent": request.headers["User-Agent"],
                    "Referer": referer
                }
            }
    return jsonify(result)


@app.post("/login")
def login():
    username = request.json.get("username", None)
    password = request.json.get("password", None)
    if not password or not username:
        result = {
            "code": 0,
            "msg": "用户名或密码必填"
        }
        return jsonify(result), 400
    else:
        if username == 'admin' and password == "123456":
            result = {
                "code": 0,
                "msg": "登录成功"
            }
            session["user"] = js.dumps({
                "id": 101,
                "name": username,
                "vip": 18
            })
            return jsonify(result)
        else:
            result = {
                "code": 1002,
                "msg": "用户名或密码错误"
            }
            return jsonify(result)


@app.get("/center")
def center():
    # user = request.cookies.get("session", None)
    user = session.get("user")
    if user:
        result = {
            "code": 0,
            "msg": "获取用户信息成功",
            "data": js.loads(user)
        }
        return jsonify(result)
    else:
        result = {
            "code": 0,
            "msg": "请登录"
        }
        return jsonify(result), 401


app.run(debug=True)
